<?php
session_start();
class buscarDisciplina extends Tpage{

	private $_data=null;
	private $_disciplina;
	private $_isPar = 0;
	
	
	public function pesquisaDisciplina($sender, $param){
		
		$paramSelecionado = $this->collectSelectionResult($this->parametro);
		$_SESSION['paramSelecionado'] = $paramSelecionado;
		$_SESSION['descricao'] = $this->descricaoBusca->Text;
		
		if($paramSelecionado == 1){
			
			
			$this->loadData();
			if($this->_data["0"]["StrDisciplina"] == null){
				$this->mensagenErro->Text = "Não foi encontrado nenhum registro";
			}else{
				$this->mensagenErro->Text = "";
			}
			$this->DataList->EditItemIndex=-1;
			$this->DataList->DataSource=$this->Data;
			$this->DataList->dataBind();
			
			
		}else if($paramSelecionado == 2){
			
			
			$this->loadData();
			
			if($this->_data["0"]["StrDisciplina"] == null){
				$this->mensagenErro->Text = "Não foi encontrado nenhum registro";
			}else{
				$this->mensagenErro->Text = "";
			}
			
			$this->DataList->EditItemIndex=-1;
			$this->DataList->DataSource=$this->Data;
			$this->DataList->dataBind();
		}
		
	}
	
	
	 protected function collectSelectionResult($input)
    {
        $indices=$input->SelectedIndices;
        $result='';
        foreach($indices as $index)
        {
            $item=$input->Items[$index];
            $result = $item->Value;
        }
        return $result;
    }

	
	protected function getData()
	{
		if($this->_data===null)
		$this->loadData();
		return $this->_data;
	}

	protected function loadData()
	{
		
		if($_SESSION['paramSelecionado'] == 1){
		
			$this->_disciplina = disciplinaRecord::finder()->findAll('FKCodLongUsuario = ?', $_SESSION['idUsuario']);
			
			foreach ($this->_disciplina as $dados=>$valor){

				$vetor []= (array(
            					'PKCodStrDisciplina'=>$valor->PKCodStrDisciplina,
            					'StrDisciplina'=>$valor->StrDisciplina,
				));
			}
			}	
			 if($_SESSION['paramSelecionado'] == 2){
				$this->_disciplina = disciplinaRecord::finder()->findbyPk($_SESSION['descricao']);
				
				$vetor []= (array(
            					'PKCodStrDisciplina'=>$this->_disciplina->PKCodStrDisciplina,
            					'StrDisciplina'=>$this->_disciplina->StrDisciplina,
				));
			}
			$this->_data=$vetor;
			$this->saveData();
	}

	protected function saveData()
	{
		$this->setViewState('Data',$this->_data);
	}

	/*
	 * carrega os dados na tabela
	 */
	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->IsPostBack)
		{
			$this->usuarioLogado->Text = $_SESSION['usuario'];
			$this->DataList->DataSource=$this->Data;
			$this->DataList->dataBind();
		}
		 
	}

	/*
	 * criar um item(usuario) da tabela
	 */
	public function itemCreated($sender,$param)
	{
		$item=$param->Item;
		if($item->ItemType==='EditItem')
		{
			// set column width of textboxes
			//$item->BookTitleColumn->TextBox->Columns=40;
			//$item->PriceColumn->TextBox->Columns=5;

		}
		if($item->ItemType==='Item' || $item->ItemType==='AlternatingItem' || $item->ItemType==='EditItem')
		{
			//janela de alerta para a exclusao
			$item->DeleteColumn->Button->Attributes->onclick='if(!confirm(\'Deseja realmente deletar essa Disciplina\')) return false;';
		}
	}

	/*
	 * deleta o usuário
	 */
	protected function deleteDisciplina($id)
	{
		 
		if($this->_data===null)
		$this->loadData();
		$deleteIndex=-1;

		foreach($this->_data as $index=>$row)
		if($row['PKCodStrDisciplina']===$id)
		$deleteIndex=$index;
		if($deleteIndex>=0)
		{
			unset($this->_data[$deleteIndex]);
			disciplinaRecord::finder()->deleteByPk($id);
			$this->saveData();

		}
	}

	/*
	 * pega o chave primera que ta na tabela que eu configurei lá no .page
	 * chama o deleteUsuario() que é responsavel por remove o item da tabela e remover o usuário do banco de dados
	 */
	public function deleteItem($sender,$param)
	{
		$this->deleteDisciplina($this->DataList->DataKeys[$param->Item->ItemIndex]);
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();

	}


	public function editItem($sender,$param)
	{		
		$this->DataList->SelectedItemIndex=-1;
		$this->DataList->EditItemIndex=$param->Item->ItemIndex;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		
		$this->DataList->SelectedItemIndex=-1;
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}



	protected function updateDisciplina($id, $name)
	{
		if($this->_data===null)
		$this->loadData();
		$updateRow=null;
		foreach($this->_data as $index=>$row)
		if($row['PKCodStrDisciplina']===$id)
		$updateRow=&$this->_data[$index];
		if($updateRow!==null)
		{
			/*
			 *Atualiza o usuário com os novos dados vindo da tabela de edição
			 *em faze de beta
			 */
			$disciplina = disciplinaRecord::finder()->findbyPk($id);
			
			echo $nome;
			echo $email;
			$disciplina->PKCodStrDisciplina = $id;
			$disciplina->StrDisciplina = $name;
			
			
			$disciplina->save();
			 
			/*
			 * atualiza a tabela da visão com os dados novos
			 */
			$updateRow['PKCodStrDisciplina']=$id;
			$updateRow['StrDisciplina']=$name;

			$this->saveData();
		}
	}

	public function updateItem($sender,$param)
	{
		$item=$param->Item;
		$this->updateDisciplina(
			$this->DataList->DataKeys[$item->ItemIndex],
			$item->StrDisciplina->Text
		);
		$this->DataList->EditItemIndex=-1;
		$this->DataList->DataSource=$this->Data;
		$this->DataList->dataBind();
	}

}

?>